import configparser
import os.path
from time import strftime

from loguru import logger


class LogUtil:
    def __init__(self):
        self.log_config_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "config",
                                            "setting.ini")
        str_time = strftime("%Y%m%d-%H")
        self.log_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "log",
                                     str_time + "log")

    def getLog(self):
        __flg = True
        if __flg:
            cfg = configparser.ConfigParser()
            cfg.read(self.log_config_path, encoding="utf-8")
            logger.add(
                self.log_path,
                format=cfg.get("log", "format"),
                level=cfg.get("log", "level"),
                rotation=cfg.get("log", "rotation"),
                retention=cfg.get("log", "retention"),
                compression=cfg.get("log", "compression"),
            )
            __flg = False
        return logger


getlog = LogUtil().getLog()
